import { parallel } from '@shared/utils';

const semiLocales: Record<string, () => Promise<any>> = {
    'zh_CN': () => import('@douyinfe/semi-ui/lib/es/locale/source/zh_CN'),
    'en_US': () => import('@douyinfe/semi-ui/lib/es/locale/source/en_US'),
};

const bizLocales: Record<string, () => Promise<any>> = {
    'zh_CN': () => import('./locales/zh_CN'),
    'en_US': () => import('./locales/en_US'),
};

export type LocaleContext = {
    locale: string;
    frameworkLocaleModule: any;
    bizLocaleModule: Record<string, string>;
}; 

const localeContext: LocaleContext = {
    locale: '',
    frameworkLocaleModule: undefined,
    bizLocaleModule: {},
};

export const initialize = async (locale: string): Promise<LocaleContext> => {
    localeContext.locale = locale;

    const semiLocale = semiLocales[locale];
    const bizLocale = bizLocales[locale];

    const [ [semiLocaleMod], [bizLocaleMod] ] = await parallel([
        semiLocale(),
        bizLocale()
    ]);

    localeContext.frameworkLocaleModule = semiLocaleMod.default;
    localeContext.bizLocaleModule = bizLocaleMod.default;

    return localeContext;
}

export const lang = (key: string): string => {
    let text = localeContext.bizLocaleModule[key];
    return text || `{{${key}}}`;
}
